package com.etc;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


public class MyFamily {
    public static class Familymap extends Mapper<LongWritable, Text,Text,Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //value:"Tom	Lucy"
            String line = value.toString();
            String[] family = line.split("   ");
            //跳过"child"的行列
            if(family[0].compareTo("child") != 0) {
                //加上后缀"1"作为表1
                context.write(new Text(family[0]), new Text(family[1] + "1"));//key是value的小孩	key:Tom	value:Lucy_1
                //加上后缀"2"作为表2
                context.write(new Text(family[1]), new Text(family[0] + "2"));//key是value的父母	key:Lucy value:Tom_2
            }
        }
    }

    public static class FamilyReduce extends Reducer<Text,Text,Text,Text> {
        int a = 0;

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //输出表头
            if (0 == a) {
                context.write(new Text("grandchild"), new Text("grandparent"));
                a++;
            }

            ArrayList<String> Grandchild = new ArrayList<String>();
            ArrayList<String> Grandparent = new ArrayList<String>();
            for (Text family : values) {
                //根据后缀"1"找到表1
                if (family.toString().endsWith("1")) {
                    //添加到Grandchild集合
                    Grandchild.add(family.toString());
                } else {
                    //添加到Grandparent集合
                    Grandparent.add(family.toString());
                }
            }
            for (String grandchild : Grandchild) {
                for (String grandparent : Grandparent) {
                    //截取0到集合总长度-1之间的字符串(目的是为了去掉后缀)
                    context.write(new Text(grandchild.substring(0, grandchild.length() - 1)), new Text(grandparent.substring(0, grandparent.length() - 1)));
                }
            }
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //对任务job的描述
        //job的jar路径
        job.setJarByClass(MyFamily.class);
        //job对应的Mapper
        job.setMapperClass(MyFamily.Familymap.class);
        //job的Reducer
        job.setReducerClass(MyFamily.FamilyReduce.class);
        //Mapper的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //Reducer的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        File file = new File("F:\\output");
        if (file.exists()) {
            FileUtils.deleteDirectory(file);
        }

        //job 处理文件路径
        FileInputFormat.setInputPaths(job, new Path("F:\\family"));
        //job 处理之后文件路径
        FileOutputFormat.setOutputPath(job, new Path("F:\\output"));
        //提交job

        job.waitForCompletion(true);
    }
}
